import Vue from 'vue'
import App from './App.vue'
import router from './routes.js'
import api from './libs/api.js'

import './assets/styles/base.css'
import * as filters from './filter'


//自适应字体大小
(function (doc, win) {
    var docEl = doc.documentElement,
      resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
      recalc = function () {
        var clientWidth = docEl.clientWidth;
        if (!clientWidth) return;
        docEl.style.fontSize = 20 * (clientWidth / 750) + 'px';
      };
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
  })(document, window);

Vue.config.debug = true;//开启错误提示

//设置全局filter
Object.keys(filters).forEach(key => {
    console.log('注册过滤器:' + key)
    Vue.filter(key, filters[key])
})


// 注册一个全局自定义指令 滚动到底部 `v-scroll`
// 使用的时候只需要添加 v-scroll="loadMore"
Vue.directive('scroll', {
    inserted: function (el, binding){
        let timerId = null, viewH, contentH, scrollTop;
        window.addEventListener('scroll', ()=> {
            timerId && clearTimeout(timerId)

            viewH = window.innerHeight,//可见高度
            contentH = document.body.scrollHeight,//内容高度
            scrollTop = document.body.scrollTop || document.documentElement.scrollTop;//滚动高度

            //if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容
            if (scrollTop /(contentH -viewH) >= 0.95) { //到达底部100px时,加载新内容
                let fnc = binding.value;   
                timerId = setTimeout(function(){
                    fnc && fnc(); 
                }, 200)
            }
        })
    }
})

// 微信签名
const wxConfig = function () {
   let url = location.href.split('#')[0]
   api.user.wxsign(url)
       .then(json => {
            let apis = [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo',
                'onMenuShareQZone',
                'hideMenuItems',
                'showMenuItems',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem',
                'translateVoice',
                'startRecord',
                'stopRecord',
                'onVoiceRecordEnd',
                'playVoice',
                'onVoicePlayEnd',
                'pauseVoice',
                'stopVoice',
                'uploadVoice',
                'downloadVoice',
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',
                'getLocalImgData',
                'getNetworkType',
                'openLocation',
                'getLocation',
                'hideOptionMenu',
                'showOptionMenu',
                'closeWindow',
                'scanQRCode',
                'chooseWXPay',
                'openProductSpecificView',
                'addCard',
                'chooseCard',
                'openCard'
            ]
            
            if (json.code == 200) {
                wx.config({
                    debug: false,
                    appId: json.data.appId,
                    timestamp: json.data.timestamp,
                    nonceStr: json.data.nonceStr,
                    signature: json.data.signature,
                    jsApiList: apis
                });
                wx.ready((e) => {
                    
                });
            }
       })
   
};

new Vue({
    router,
    el: '#app',
    render: h => h(App)
})

// 获取微信JSSDK配置
wxConfig();











